/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: userLogin.c
> Author: KAIJIE.JIANG
> Mail: 
> Created Time: 2024年07月19日 星期五 00时16分00秒
***************************************************/

#include "userLogin.h"

//创建头节点
UL * createUserLoginHead(void){
	UL *head = NULL;
	head = (UL*)malloc(sizeof(UL));
	memset(head,0,sizeof(UL));
	return head;
}

//创建链表 -- 输入0停止
UL * createUserLoginLink(UL *head){
	assert(head);
	UL *cur = NULL;
	UL *new = NULL;
	cur = head;
	char str[20] = {"1"};

	FILE *fp_vipInfo = NULL;
	VIP_M *head_vipInfo = NULL;

	fp_vipInfo = fopen("./vipInfo.txt","r");
	if(fp_vipInfo == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}

	head_vipInfo = createManagerHead();
	head_vipInfo = vipInfoReadFile(fp_vipInfo,head_vipInfo);

	fclose(fp_vipInfo);

	VIP_M *cur_vipInfo = NULL;
	cur_vipInfo = head_vipInfo->next;

	while(cur_vipInfo != NULL){
		new = (UL*)malloc(sizeof(UL));
		memset(new,0,sizeof(UL));

		new->data.id = cur_vipInfo->data.id;
		strcpy(new->data.userPassword,str);
		new->data.state = cur_vipInfo->data.state;

		cur->next = new;
		new->next = NULL;
		cur =cur->next;

		cur_vipInfo = cur_vipInfo->next;
	}

	return head;
}

//输出链表
void outputUserLoginLink(UL *head){
	assert(head);
	UL * cur = NULL;
	cur = head->next;

	while(cur != NULL){
		printf("%d\t%s\n",cur->data.id,cur->data.userPassword);
		cur = cur->next;
	}
}

//修改链表
UL * amendUserLoginLink(UL * head){
	assert(head);
	UL * cur = NULL;
	cur = head->next;

	char dest[20] = {0};
	int flag = 0;
	int n = 0;

	printf("修改密码\n");
	printf("会员id：");
	scanf("%d",&n);

	while(cur != NULL){
		if(cur->data.id == n){
			flag = 1;
			while(1){
				bzero(dest,sizeof(dest));
				printf("输入旧密码：");
				scanf("%s",dest);
				if(strcmp(cur->data.userPassword,dest) == 0){
					printf("输入新密码：");
					scanf("%s",cur->data.userPassword);
					break;
				}else{
					printf("旧密码错误！\n");
				}
			}
			break;
		}
		cur = cur->next;
	}
	if(flag == 0){
		printf("未找到账号！\n");
	}

	return head;
}


//释放链表
void freeUserLoginLink(UL * head){
	assert(head!=NULL);
	
	UL *temp = NULL;
	temp = head;
	UL *cur = NULL;
	while(temp!=NULL)
	{
		//cur 存放下一个节点
		cur = temp->next;
		free(temp);
		temp = NULL;
		temp = cur;
	}
}

//将链表信息保存为文件
FILE * saveUserLoginLinkToFile(FILE * fp,UL * head){
	UL *cur = NULL;
	cur = head->next;
	while(cur != NULL){
		fwrite(cur,sizeof(UL),1,fp);
	
		cur = cur->next;
	}
	cur = NULL;
	return fp;
}

//将文件读到链表
UL *readUserLoginFileToLink(FILE *fp,UL *head){
	assert(head);
	UL *cur = NULL;
	UL *new = NULL;
	cur = head;

	size_t ld = 1;
	while(ld != 0){
		new = (UL*)malloc(sizeof(UL));
		memset(new,0,sizeof(UL));

		ld = fread(new,sizeof(UL),1,fp);

		if(ld == 0){
			free(new);
			new = NULL;
			break;
		}
//		printf("id:%d\t,userPassword:%s\n",new->data.id,new->data.userPassword);
		cur->next = new;
		new->next = NULL;
		cur = cur->next;
	}
	return head;
}

//登录函数 返回值：id
int loginUserSystem(UL *head){
	UL *cur = 0;
	char dest[20] = {0};
	int n = 0;

	int flag = 0;
	//判断可否找到UL，找到flag1=1，找不到flag1=0
	int flag1 = 0;
	while(1){
		n = 0;
		printf("id:");
		scanf("%d",&n);
		if(n >= 99999 && n <= 1000000){
			cur = head->next;
			while(cur != NULL){
				if(cur->data.id == n){
					flag1 = 1;
					break;
				}
				cur = cur->next;
			}
			if(cur->data.state == 0){
				printf("账号处于挂失状态！\n");
				printf("请至管理员处解除挂失后登录！\n");
				break;
			}else{
				//判断密码是否正确
				if(flag1 == 1){
					printf("userPassword:");
					scanf("%s",dest);

					if(strcmp(cur->data.userPassword,dest) == 0){
						flag = n;
						printf("登录成功！\n");
						break;	
					}else{
						printf("密码输入错误！\n");
						continue;
					}
				}else{
					printf("未找到会员账号！\n");
				}

			}
		}else{
			printf("id输入错误！\n");
		}
	}
	cur = NULL;
	return flag;
}

//会员账号注册函数
void registerUser(FILE *fp,UL *head){
	head = createUserLoginHead();
	head = createUserLoginLink(head);
	outputUserLoginLink(head);

	fp = fopen("./userLogin.txt","w");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}

	fp = saveUserLoginLinkToFile(fp,head);

	fclose(fp);
	freeUserLoginLink(head);
	printf("会员账号注册成功！\n");
}

//会员账号注册添加函数
void registerAddUser(FILE *fp,UL *head){
	printf("会员账号注册：\n");
	head = createUserLoginHead();
	head = createUserLoginLink(head);
	outputUserLoginLink(head);

	fp = fopen("./userLogin.txt","w");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}

	fp = saveUserLoginLinkToFile(fp,head);

	fclose(fp);
	freeUserLoginLink(head);
	printf("会员账号注册成功！\n");
}
